﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Mime
{
    using System;
    using System.Collections.Generic;

    /// <summary>Processes a parameterized header field.</summary>
    internal sealed class ParameterizedHeaderFieldBodyProcessor : HeaderFieldBodyProcessor
    {
        private readonly List<char> currentValue = new List<char>(64);
        private readonly List<char> currentName = new List<char>(64);
        private List<char> currentString;
        private readonly ParameterizedHeaderField result;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal ParameterizedHeaderFieldBodyProcessor(string fieldName)
        {
            this.currentString = this.currentValue;
            this.result = new ParameterizedHeaderField(fieldName, null);
        }

        internal sealed override void ProcessNormalBodyEvent(byte ev)
        {
            switch (ev)
            {
                case (byte)' ':
                case (byte)'\t':
                    break;
                case (byte)';':
                    this.AddValueIfValid();
                    this.currentString = this.currentName;
                    break;
                case (byte)'=':
                    this.currentString = this.currentValue;
                    break;
                default:
                    this.currentString.Add((char)ev);
                    break;
            }
        }

        internal sealed override void ProcessQuotedBodyEvent(byte ev, bool isQuoteChar)
        {
            if (!isQuoteChar)
            {
                this.currentString.Add((char)ev);
            }
        }

        internal sealed override void ProcessCommentBodyEvent(byte ev)
        {
        }

        internal sealed override HeaderField GetHeaderField()
        {
            this.AddValueIfValid();
            return this.result;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private void AddValueIfValid()
        {
            if (object.ReferenceEquals(this.currentString, this.currentValue))
            {
                string val = new string(this.currentValue.ToArray());

                if (this.result.Value == null)
                {
                    this.result.Value = val;
                }
                else
                {
                    this.result.Parameters.Add(new string(this.currentName.ToArray()), val);
                }
            }

            this.currentValue.Clear();
            this.currentName.Clear();
        }
    }
}
